package mul.UI;

import java.util.HashMap;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.media.SoundPool.OnLoadCompleteListener;

public class SoundManager {
	public static final int BUTTON = 1;
	public static final int CRATEEXPLOSION = 2;
	public static final int EVIL_LAUGHTER = 3;
	
	private SoundPool soundPool;
	private MediaPlayer mediaPlayer;
	private HashMap <Integer, Integer> soundPoolMap;
	private Context context;
	
	private boolean wrongAnswerIsPlaying;
	
	public SoundManager(Context context){
		this.context = context;
		
//		Should make some kind of check for loading sounds here.
//		Currently room for 10 songs, more can be added.
//		Use soundpool for sounds and effects. 
		soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
		soundPoolMap = new HashMap <Integer, Integer>();
		soundPoolMap.put(BUTTON, soundPool.load(context, R.raw.button, 1));
		soundPoolMap.put(CRATEEXPLOSION, soundPool.load(context, R.raw.explosion, 1));
		soundPoolMap.put(EVIL_LAUGHTER, soundPool.load(context,R.raw.evillaugh, 1));
//		Mediaplayer for songs :)
		this.mediaPlayer = MediaPlayer.create(context, R.raw.emergeinlove);
		this.wrongAnswerIsPlaying = false;
	}
	
	public void playSound(int sound){
		AudioManager mgr = (AudioManager)this.context.getSystemService(context.AUDIO_SERVICE);
		float streamVolumeCurrent = mgr.getStreamVolume(AudioManager.STREAM_MUSIC);
		float streamVolumeMax = mgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
		float volume = 10.0f;
		
		soundPool.play(this.soundPoolMap.get(sound), volume, volume, 1, 0, 1f);
	}
	
	public void playThemeSong(){
		if(!mediaPlayer.isPlaying()){
			this.mediaPlayer.start();
			this.mediaPlayer.setLooping(true);
		}
	}
	
	public void playNewCrate(){
		this.playSound(BUTTON);
	}
	public void playCrateExplosion(){
		
		this.playSound(CRATEEXPLOSION);
	}
	
	public void playWrongAnswer(){
		
		this.playSound(EVIL_LAUGHTER);
	}
	
	public void stopThemeSong(){
		
		this.mediaPlayer.stop();
	}
}
